<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 4.3.2 or newer
 *
 * @package		CodeIgniter
 * @author		Dennis Kaplan
 * @copyright	Copyright (c) 2007, PzzAzz LLC
 * @license		http://www.codeignitor.com/user_guide/license.html
 * @link		http://www.codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * CodeIgniter Cookie Helpers
 *
 * @package		CodeIgniter
 * @subpackage	Helpers
 * @category	Helpers
 * @author		Rick Ellis
 * @link		http://www.codeigniter.com/user_guide/helpers/cookie_helper.html
 */

// ------------------------------------------------------------------------

/**
 * Set cookie
 *
 * Accepts six parameter, or you can submit an associative
 * array in the first parameter containing all the values.
 *
 * @access	public
 * @param	mixed
 * @param	string	the value of the cookie
 * @param	string	the number of seconds until expiration
 * @param	string	the cookie domain.  Usually:  .yourdomain.com
 * @param	string	the cookie path
 * @param	string	the cookie prefix
 * @return	void
 */
 

function cfg_cookie()
{
	$CI =& get_instance();
		return array(
            'name'   => '',
            'value'  => '',
            'expire' => '',
            'domain' => $CI->config->item('cookie_domain'),
            'path'   => $CI->config->item('cookie_path'),
            'prefix' => $CI->config->item('cookie_prefix'),
            'secure' => FALSE,
            'httponly' => FALSE
			);	
}

function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '', $prefix = '', $secure = FALSE, $httponly = TRUE)
{
    $set = '';
    $cfg = cfg_cookie();
    
    if (is_array($name)){
        $set = $name;
        unset($name);
    }else{
        $set = array(
        'name'   => $name,
        'value'  => $value,
        'expire' => $expire,
        'domain' => $domain,
        'path'   => $path,
        'prefix' => $prefix,
        'secure' => $secure,
        'httponly' => $httponly
        );
        
    }

    foreach($cfg as $k=>$v)
    {
    	if(($set[$k]) == '' || (!isset($set[$k])))
        {
            $set[$k] = $cfg[$k];
        }
    }
    
    extract($set, EXTR_OVERWRITE);

    if (is_numeric($expire))
    {
        $expire = time() + $expire;
    }
    else
    {
        $expire = time()+60*60*24*3;
    }

	setcookie($prefix.$name, $value, $expire, $path, $domain, 0);
}
	
// --------------------------------------------------------------------

/**
 * Fetch an item from the COOKIE array
 *
 * @access	public
 * @param	string
 * @param	bool
 * @return	mixed
 */
function get_cookie($index = '', $xss_clean = FALSE)
{
	$CI =& get_instance();
    $prefix=$CI->config->item('cookie_prefix');
	return $CI->input->cookie($prefix.$index, $xss_clean);
}

// --------------------------------------------------------------------

/**
 * Delete a COOKIE
 *
 * @param	mixed
 * @param	string	the cookie domain.  Usually:  .yourdomain.com
 * @param	string	the cookie path
 * @param	string	the cookie prefix
 * @return	void
 */
function del_cookie($name, $domain = '', $path = '/', $prefix = '')
{
    delete_cookie($name, $domain = '', $path = '/', $prefix = '');
}
function delete_cookie($name, $domain = '', $path = '/', $prefix = '')
{
    if (is_array($name)){
        $set = $name;
    }else{
        $set = array(
        'name'   => $name,
        'value'  => 'Delete',
        'expire' => -60*60*24*365,
        'domain' => $domain,
        'path'   => $path,
        'prefix' => $prefix
        );
        
    }
    extract($set, EXTR_OVERWRITE);
    set_cookie($name, $value, $expire, $domain, $path, $prefix);
}


?>